﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.NhapKho
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion

        #region Khởi tạo
        private Entities.HienThiHangHoaListview rowlistview;
        private string ManhanVien = string.Empty;
        private string TenNhanVien = string.Empty;
        private DateTime date;
        private string MaHoaDonNhap = string.Empty;
        private string MaKho = string.Empty;
        private string TenKho = string.Empty;
        private Entities.HienThiHangHoaListview[] DanhSach;
        private Entities.HienThiHangHoaListview timhanghoa;
        private string hanhDong;
        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }

        private Entities.HoaDonNhap hoadonnhap;
        public Entities.HoaDonNhap Hoadonnhap
        {
            get { return hoadonnhap; }
            set { hoadonnhap = value; }
        }

        public frmXuLy()
        {
            InitializeComponent();

        }

        public frmXuLy(string hanhDong, Entities.HoaDonNhap hoadonnhap)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.hoadonnhap = hoadonnhap;
        }
        #endregion

        #region Load
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            try
            {
                cbxLoaiNhap.SelectedIndex = 1;
                cbxLoaiThanhToan.SelectedIndex = 0;
                try
                {
                    date = Common.StaticVariable.InterfaceObject.SelectDate();
                }
                catch (Exception)
                {
                    date = DateTime.Now;
                }
                LayCaiDatKho();
                //Select();
                switch (this.hanhDong)
                {
                    case "Them":
                        {
                            this.MaHoaDonNhap = GetID();
                            GridView();
                        }
                        break;
                    case "Xem":
                        {
                            this.Binding(this.hoadonnhap);
                            panel_TinhToan.Enabled = false;
                            btnGhiLai.Enabled = false;
                            dgvHienThi.Enabled = false;
                            TopHienThi.Enabled = false;
                        }
                        break;
                    default:
                        this.Thoat();
                        break;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Hien thi
        private void GridView()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.HienThiHangHoaListview[] nv = new Entities.HienThiHangHoaListview[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["TenHangHoa"].HeaderText = "Tên hàng hóa";
                dgvHienThi.Columns["TenHangHoa"].Visible = true;
                dgvHienThi.Columns["TenDonViTinh"].HeaderText = "ĐVT";
                dgvHienThi.Columns["TenDonViTinh"].Visible = true;
                dgvHienThi.Columns["HanSuDung"].HeaderText = "Hạn sử dụng";
                dgvHienThi.Columns["HanSuDung"].Visible = true;
                dgvHienThi.Columns["GiaNhap"].HeaderText = "Giá";
                dgvHienThi.Columns["GiaNhap"].Visible = true;
                dgvHienThi.Columns["SoLuong"].HeaderText = "Số lượng";
                dgvHienThi.Columns["SoLuong"].Visible = true;
                dgvHienThi.Columns["ChietKhau"].HeaderText = "Chiết khấu";
                dgvHienThi.Columns["ChietKhau"].Visible = true;
                dgvHienThi.Columns["Thue"].HeaderText = "Thuế";
                dgvHienThi.Columns["Thue"].Visible = true;
                dgvHienThi.Columns["TongDaChietKhau"].HeaderText = "Thành tiền (đã C/K)";
                dgvHienThi.Columns["TongDaChietKhau"].Visible = true;
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception)
            {
            }
        }
        #endregion

        #region Đổ dữ liệu xem
        private void Binding(Entities.HoaDonNhap hoadon)
        {
            try
            {
                this.MaKho = hoadon.MaKho;
                this.TenKho =hoadon.TenKho;
                this.MaHoaDonNhap = hoadon.MaHoaDonNhap;
                lblTenNhanVien.Text =hoadon.TenNhanVien;
                cbxLoaiNhap.Items.Add("Nhập theo đơn đặt hàng");
                cbxLoaiNhap.Items.Add("Không theo đơn đặt hàng");
                if (hoadon.MaDonDatHang != string.Empty)
                {
                    cbxLoaiNhap.SelectedIndex = 0;
                }
                else
                {
                    cbxLoaiNhap.SelectedIndex = 1;
                }
                txtMaNhaCungCap.Text = hoadon.MaNhaCungCap;
                txtTenNhaCungCap.Text = hoadon.TenNhaCungCap;
                txtNoHienThoi.Text = hoadon.NoHienThoi;
                txtNguoiGiaoHang.Text = hoadon.NguoiGiaoHang;
                try
                {
                    txtHanThanhToan.Text = hoadon.HanThanhToan.ToString("MM/dd/yyyy");
                    if (txtHanThanhToan.Text == string.Empty)
                    {
                        txtHanThanhToan.Text = hoadon.HanThanhToan.ToString("MM/dd/yyyy");
                    }
                }
                catch
                { txtHanThanhToan.Text = hoadon.HanThanhToan.ToString("MM/dd/yyyy"); }
                cbxLoaiThanhToan.Items.Add("Tiền Mặt");
                cbxLoaiThanhToan.Items.Add("ATM");
                if (hoadon.HinhThucThanhToan == "ATM")
                {
                    cbxLoaiThanhToan.SelectedIndex = 1;
                }
                else
                {
                    cbxLoaiThanhToan.SelectedIndex = 0;
                }
              
                txtMaDonDatHang.Text = hoadon.MaDonDatHang;

                Entities.HienThiHangHoaListview ncc = new Entities.HienThiHangHoaListview();
                ncc.HanhDong = "4";
                ncc.MaKho = "";
                ncc.MaNhaCungCap = "";
                ncc.MaHoaDon = this.hoadonnhap.MaHoaDonNhap;
                DanhSach = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaCacBang(ncc);
                dgvHienThi.DataSource = this.DanhSach;
                this.GridView();
                HienThiTinhToan();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region Hiển thị

        private void ResetNhaCungCap()
        {
            txtMaNhaCungCap.Text = "";
            txtNoHienThoi.Text = "0";
            txtTenNhaCungCap.Text = "";
        }

        private void ResetTool()
        {
            try
            {
                txtMaHangHoa.Text = "<F4 - Tra cứu>";
                txtGia.Text = "0";
                txtSoLuong.Text = "1";
                txtChietKhauNhaCungCap.Text = "0";
                txtThueVAT.Text = "0";
                txtHanSuDung.Text = date.ToString("MM/dd/yyyy");
            }
            catch (Exception)
            {
            }
        }
        #endregion

        #region Cai Dat Kho
        private void LayCaiDatKho()
        {
            try
            {
                SetupConfig.Variable vl = new SetupConfig.Variable();
                vl.HanhDong = "1";
                //vl.HanhDong = "2";
                System.Reflection.Assembly getfile = System.Reflection.Assembly.GetExecutingAssembly();
                vl.FileName = getfile.Location + ".config";
                //vl.KeyName = "ChatService";
                SetupConfig.Variable[] list = SetupConfig.Config.GetConfig(vl);
                this.MaKho = list[0].KeyValue;
                this.TenKho = list[1].KeyValue;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Lay Ma

        private string GetID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "HoaDonNhap";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);
                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "HDN_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "HDN_0001";
            }
            return code;
        }
        #endregion

        #region Tim Kiem
      

        private void TimNhaCungCap()
        {
            try
            {
                GUI.TimKiem.frmTimKiemNhaCungCap frm = new TimKiem.frmTimKiemNhaCungCap("NhapKho");
                frm.ShowDialog();
                if (Common.StaticVariable.nhacungcap != null)
                {
                    txtMaNhaCungCap.Text = Common.StaticVariable.nhacungcap.MaNhaCungCap;
                    txtNoHienThoi.Text = Common.StaticVariable.nhacungcap.NoHienThoi;
                    txtTenNhaCungCap.Text = Common.StaticVariable.nhacungcap.TenNhaCungCap;
                    txtNguoiGiaoHang.Focus();
                }
                else
                {
                    txtMaNhaCungCap.Text = "<F4 - Tra cứu>";
                    txtNoHienThoi.Text = "0";
                    txtTenNhaCungCap.Text = "";
                    txtMaNhaCungCap.Focus();
                }
            }
            catch (Exception)
            {
            }
        }

        private void TimNDonDatHang()
        {
            try
            {
                GUI.TimKiem.frmTimKiemDonDatHang frm = new TimKiem.frmTimKiemDonDatHang("NhapKho", this.MaKho,txtMaNhaCungCap.Text);
                frm.ShowDialog();
                if (Common.StaticVariable.dathangnhacungcap != null)
                {
                    txtMaDonDatHang.Text = Common.StaticVariable.dathangnhacungcap.MaDonDatHangNCC;
                    txtNgayDatHang.Text = Common.StaticVariable.dathangnhacungcap.NgayDonHang.ToString("MM/dd/yyyy");
                    cbxLoaiThanhToan.Focus();
                    ChiTietDonDatHang(Common.StaticVariable.dathangnhacungcap.MaDonDatHangNCC);
                    HienThiTinhToan();
                    txtThanhToanNgay.Text = "0";
                }
                else
                {
                    txtMaDonDatHang.Text = "<F4 - Tra cứu>";
                    txtNgayDatHang.Text = "mm/dd/yyyy";
                    txtMaDonDatHang.Focus();
                }
            }
            catch (Exception)
            {
            }
        }

        private void TimHangHoa()
        {
            try
            {
                if (this.MaKho == string.Empty || this.MaKho==null)
                {
                    MessageBox.Show("Chưa cài đặt kho hàng","Thông báo");
                    return;
                }
                if (txtMaNhaCungCap.Text == string.Empty || txtMaNhaCungCap.Text == "<F4 - Tra cứu>")
                {
                    MessageBox.Show("Nhập mã nhà cung cấp", "Thông báo");
                    return;
                }
                switch(cbxLoaiNhap.SelectedIndex)
                {
                    case 1:
                        {
                            GUI.TimKiem.frmTimKiemHangHoaTheoKho frm = new TimKiem.frmTimKiemHangHoaTheoKho("TheoNhaCungCap", this.txtMaNhaCungCap.Text);
                            frm.ShowDialog();
                        }
                        break;
                    case 0:
                        {
                            GUI.TimKiem.frmTimKiemHangHoaTheoKho frm = new TimKiem.frmTimKiemHangHoaTheoKho("TheoNhaCungCap", this.txtMaNhaCungCap.Text);
                            frm.ShowDialog();
                        }
                        break;
                }
                if (Common.StaticVariable.ChiTietHangHoaCacBang != null)
                {
                    this.timhanghoa = Common.StaticVariable.ChiTietHangHoaCacBang;
                    txtMaHangHoa.Text = this.timhanghoa.MaHangHoa;
                    txtGia.Text = this.timhanghoa.GiaNhap;
                    txtSoLuong.Text = "1";
                    txtChietKhauNhaCungCap.Text = "0";
                    txtThueVAT.Text = timhanghoa.Thue;
                    txtSoLuong.Focus();
                    Common.StaticVariable.ChiTietHangHoaCacBang = null;
                }
                else
                {
                    timhanghoa = null;
                    txtMaHangHoa.Text = "<F4 - Tra cứu>";
                    txtSoLuong.Text = "1";
                    txtChietKhauNhaCungCap.Text = "0";
                    txtThueVAT.Text = "0";
                    txtMaHangHoa.Focus();
                }
            }
            catch (Exception)
            {
            }
        }
        #endregion

        #region Event
        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckString(e);
        }

        private void txtChietKhauNhaCungCap_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckString(e);
        }

        private void txtThanhToanNgay_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckString(e);
        }

        private void txtNguoiGiaoHang_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckNumber(e);
        }

        private void btnGhiLai_Click(object sender, EventArgs e)
        {
            this.Them();
        }

        private void cbxLoaiNhap_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.ResetTool();
                switch (cbxLoaiNhap.SelectedIndex)
                {
                    //Nhập kho theo đơn đặt hàng  --> index = 0
                    //Nhập kho không theo đơn đặt hàng  --> index=1
                    case 0:
                        btnTimDonDatHang.Enabled = true;
                        txtMaHangHoa.Enabled = false;
                        txtSoLuong.Enabled = false;
                        txtGia.Enabled = false;
                        txtThueVAT.Enabled = false;
                        txtMaDonDatHang.Text = "<F4 - Tra cứu>";
                        txtNgayDatHang.Text = "";
                        timhanghoa = null;
                        Common.StaticVariable.dathangnhacungcap = null;
                        DanhSach = null;
                        rowlistview = null;
                        Common.StaticVariable.ChiTietHangHoaCacBang = null;
                        this.GridView();
                        txtThanhTien.Text = "0";
                        txtChietKhau.Text = "0";
                        txtThue.Text = "0";
                        txtTongTien.Text = "0";
                        txtThanhToanNgay.Text = "0";
                        txtNoLai.Text = "0";
                        break;
                    case 1:
                        btnTimDonDatHang.Enabled = false;
                        txtMaHangHoa.Enabled = true;
                        txtSoLuong.Enabled = true;
                        txtGia.Enabled = true;
                        txtThueVAT.Enabled = true;
                        txtMaDonDatHang.Text = "<F4 - Tra cứu>";
                        txtNgayDatHang.Text = "";
                        timhanghoa = null;
                        Common.StaticVariable.dathangnhacungcap = null;
                        DanhSach = null;
                        rowlistview = null;
                        Common.StaticVariable.ChiTietHangHoaCacBang = null;
                        this.GridView();
                        txtThanhTien.Text = "0";
                        txtChietKhau.Text = "0";
                        txtThue.Text = "0";
                        txtTongTien.Text = "0";
                        txtThanhToanNgay.Text = "0";
                        txtNoLai.Text = "0";
                        break;
                }
            }
            catch (Exception)
            {
            }

        }

        private void txtHanSuDung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    HienThiTheoNhaCungCap(txtMaHangHoa.Text);
                    if (timhanghoa == null)
                    {
                        txtMaHangHoa.Focus();
                        MessageBox.Show("Không tìm thấy hàng hóa này", "Thông báo");
                        ResetTool();
                        return;
                    }
                    this.Add();
                    dgvHienThi.DataSource = this.DanhSach;
                    this.GridView();
                    this.ResetTool();
                    txtMaHangHoa.Focus();

                    timhanghoa = null;
                    rowlistview = null;
                }
                catch (Exception)
                {
                }
            }
        }

        private void txtSoLuong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtChietKhauNhaCungCap.Focus();
            }
        }

        private void txtChietKhauNhaCungCap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtHanSuDung.Focus();
            }
        }

        private void txtMaHangHoa_TextChanged(object sender, EventArgs e)
        {
            if (txtMaNhaCungCap.Text == string.Empty || txtMaNhaCungCap.Text == "<F4 - Tra cứu>")
            {
                MessageBox.Show("Bạn phải chọn nhà cung cấp", "Thông báo");
                return;
            }
        }

        private void txtThanhToanNgay_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.hanhDong == "Them")
                {
                    if (txtThanhToanNgay.Text == string.Empty)
                    { return; }
                    if (double.Parse(txtTongTien.Text) > 0)
                    {
                        if (double.Parse(txtTongTien.Text) - double.Parse(txtThanhToanNgay.Text) < 0)
                        {
                            txtThanhToanNgay.Text = txtTongTien.Text;
                            txtNoLai.Text = "0";
                            return;
                        }
                        txtNoLai.Text = (double.Parse(txtTongTien.Text) - double.Parse(txtThanhToanNgay.Text)).ToString();
                    }
                    else
                    {
                        if (double.Parse(txtTongTien.Text) == 0)
                        { txtThanhToanNgay.Text = "0"; txtNoLai.Text = "0"; return; }
                        MessageBox.Show("Tổng tiền phải nhỏ hơn 0 bạn mới được nhập", "Thông báo");
                        txtThanhToanNgay.Text = "0";
                        return;
                    }
                }
            }
            catch
            {
                txtNoLai.Text = txtTongTien.Text;
                txtThanhToanNgay.Text = "";
                txtThanhToanNgay.Focus();
            }
        }

        private void txtThanhToanNgay_Click(object sender, EventArgs e)
        {
            txtThanhToanNgay.Text = "";
            txtNoLai.Text = txtTongTien.Text;
        }

        private void Edit(Entities.HienThiHangHoaListview row)
        {
            try
            {
                txtMaHangHoa.Text = row.MaHangHoa;
                txtGia.Text = row.GiaNhap;
                txtSoLuong.Text = row.SoLuong;
                txtChietKhauNhaCungCap.Text = row.ChietKhau;
                txtThueVAT.Text = row.Thue;
                txtHanSuDung.Text = row.HanSuDung;
                ArrayList list = new ArrayList();
                if (DanhSach != null)
                {
                    for (int i = 0; i < DanhSach.Length; i++)
                    {
                        if (DanhSach[i].MaHangHoa.ToUpper() != row.MaHangHoa.ToUpper())
                        {
                            list.Add(DanhSach[i]);
                        }
                    }
                }
                DanhSach = null;
                int k = list.Count;
                if (k != 0)
                {
                    DanhSach = new Entities.HienThiHangHoaListview[k];
                    for (int i = 0; i < k; i++)
                    {
                        DanhSach[i] = (Entities.HienThiHangHoaListview)list[i];
                    }
                }
                dgvHienThi.DataSource = this.DanhSach;
                GridView();
                HienThiTinhToan();
                txtThanhToanNgay.Text = "0";
            }
            catch (Exception)
            {
            }
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                timhanghoa = (Entities.HienThiHangHoaListview)dgvHienThi.CurrentRow.DataBoundItem;
                if (timhanghoa != null)
                {
                    Edit(timhanghoa);
                }
            }
            catch (Exception)
            {
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                HienThiTheoNhaCungCap(txtMaHangHoa.Text);
                if (timhanghoa == null)
                {
                    txtMaHangHoa.Focus();
                    MessageBox.Show("Không tìm thấy hàng hóa này", "Thông báo");
                    ResetTool();
                    return;
                }
                this.Add();
                dgvHienThi.DataSource = this.DanhSach;
                this.GridView();
                this.ResetTool();
                txtMaHangHoa.Focus();
                timhanghoa = null;
                rowlistview = null;
            }
            catch (Exception)
            {
            } 
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void txtMaNhaCungCap_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F4:
                    {
                        ResetNhaCungCap();
                        TimNhaCungCap();
                    }
                    break;
            }
            
        }

        private void btnTimNhaCungCap_Click(object sender, EventArgs e)
        {
            try
            {
                ResetNhaCungCap();
                TimNhaCungCap();
            }
            catch (Exception)
            {
            }
        }

        private void txtMaDonDatHang_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (txtMaNhaCungCap.Text == string.Empty || txtMaNhaCungCap.Text == "<F4 - Tra cứu>")
                {
                    MessageBox.Show("Bạn phải chọn nhà cung cấp", "Thông báo");
                    return;
                }
                if (cbxLoaiNhap.SelectedIndex == 0)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.F4:
                            {
                                txtMaDonDatHang.Text = "";
                                txtNgayDatHang.Text = "mm/dd/yyyy";
                                TimNDonDatHang();
                            }
                            break;
                    }
                }
                else
                { MessageBox.Show("Bạn phải chọn nhập theo đơn đặt hàng", "Thông báo"); return; }
            }
            catch (Exception)
            {
            }
        }

        private void btnTimDonDatHang_Click(object sender, EventArgs e)
        {
            if (txtMaNhaCungCap.Text == string.Empty || txtMaNhaCungCap.Text == "<F4 - Tra cứu>")
            {
                MessageBox.Show("Bạn phải chọn nhà cung cấp","Thông báo");
                return;
            }
            if (cbxLoaiNhap.SelectedIndex == 0)
            {
                txtMaDonDatHang.Text = "";
                txtNgayDatHang.Text = "mm/dd/yyyy";
                TimNDonDatHang();
            }
            else
            { MessageBox.Show("Bạn phải chọn nhập theo đơn đặt hàng", "Thông báo"); return; }
        }
        private Boolean CheckKey = false;
        private void txtMaHangHoa_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (cbxLoaiNhap.SelectedIndex == 1)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.F4:
                            {
                                CheckKey = true;
                                ResetTool();
                                txtMaHangHoa.Text = "";
                                TimHangHoa();
                            }
                            break;
                        case Keys.Enter:
                            {
                                CheckKey = false;
                                HienThiTheoNhaCungCap(txtMaHangHoa.Text);
                                if (timhanghoa == null)
                                {
                                    txtMaHangHoa.Focus();
                                    MessageBox.Show("Không tìm thấy hàng hóa này", "Thông báo");
                                    ResetTool();
                                    return;
                                }
                            }
                            break;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void txtMaHangHoa_Click(object sender, EventArgs e)
        {
            ResetTool();
            txtMaHangHoa.Text = "";
        }

        #endregion

        #region Validate
        private Boolean Validate(Entities.HoaDonNhap giatri)
        {
            Boolean kt = true;
            try
            {
                if (giatri.MaHoaDonNhap == string.Empty)
                {
                    MessageBox.Show("Bạn phải nhập mã hóa đơn", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    kt = false;
                    return kt;
                }
                if (giatri.MaKho == string.Empty)
                {
                    MessageBox.Show("Bạn phải nhập mã kho hàng", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    kt = false;
                    return kt;
                }
                if (giatri.MaNhaCungCap == string.Empty)
                {
                    MessageBox.Show("Bạn phải nhập mã nhà cung cấp", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    kt = false;
                    return kt;
                }
                if (giatri.NoHienThoi == string.Empty)
                {
                    MessageBox.Show("Bạn phải nhập nợ hiện thời", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    kt = false;
                    return kt;
                }
                if (giatri.HinhThucThanhToan == string.Empty)
                {
                    MessageBox.Show("Bạn phải chọn hình thức thanh toán", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    kt = false;
                    return kt;
                }
                //if (giatri.MaTienTe == string.Empty)
                //{
                //    MessageBox.Show("Bạn phải chọn tiền tệ", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                //    kt = false;
                //    return kt;
                //}
                if (DateTime.Compare(giatri.HanThanhToan.Date, date.Date) < 0)
                {
                    MessageBox.Show("Hạn thanh toán phải lớn hơn hoặc bằng ngày hiện tại", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                    kt = false;
                    return kt;
                }
                if (this.KiemTraHangHoa() == false)
                {
                    kt = false;
                    return kt;
                }
            }
            catch (Exception ex)
            { string s = ex.Message; kt = false; }
            return kt;
        }
        private Boolean KiemTraHangHoa()
        {
            Boolean kt = true;
            try
            {
                for (int k = 0; k < DanhSach.Length; k++)
                {
                    if (DanhSach[k].MaHangHoa == string.Empty)
                    {
                        kt = false;
                        MessageBox.Show("Mã hàng hóa " + DanhSach[k].TenHangHoa + " không được trống", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                        break;
                    }
                    if (DanhSach[k].TenHangHoa == string.Empty)
                    {
                        kt = false;
                        MessageBox.Show("Tên hàng hóa không được để trống", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                        break;
                    }
                    if (float.Parse(DanhSach[k].SoLuong) <= 0)
                    {
                        kt = false;
                        MessageBox.Show("Số lượng hàng hóa " + DanhSach[k].TenHangHoa + " phải lớn hơn 0", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                        break;
                    }
                    if (double.Parse(DanhSach[k].GiaNhap) <= 0)
                    {
                        kt = false;
                        MessageBox.Show("Giá của hàng hóa " + DanhSach[k].TenHangHoa + " phải lớn hơn 0", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                        break;
                    }
                    if (DateTime.Compare(DateTime.Parse(DanhSach[k].HanSuDung).Date, date.Date) < 0)
                    {
                        kt = false;
                        MessageBox.Show("Hạn sử dụng của hàng hóa " + DanhSach[k].TenHangHoa + " không được nhỏ hơn ngày hiện tại", Common.Constants.GiaTriCacForm.HeThongCanhBao);
                        break;
                    }
                }
            }
            catch (Exception ex)
            { string s = ex.Message; kt = false; }
            return kt;
        }
        #endregion

        #region Lấy giá trị
        private Entities.HoaDonNhap GetValue(string hanhdong)
        {
            Entities.HoaDonNhap giatri = new Entities.HoaDonNhap();
            try
            {
                giatri.HanhDong = hanhdong;
                giatri.MaHoaDonNhap = this.MaHoaDonNhap;
                giatri.NgayNhap = date.Date;
                giatri.MaNhaCungCap = txtMaNhaCungCap.Text;
                giatri.TenNhaCungCap = txtTenNhaCungCap.Text;
                giatri.NoHienThoi = txtNoHienThoi.Text;
                giatri.NguoiGiaoHang = txtNguoiGiaoHang.Text;
                giatri.HinhThucThanhToan = cbxLoaiThanhToan.Text;
                giatri.MaKho = this.MaKho;
                giatri.TenKho = this.TenKho;
                giatri.HanThanhToan = DateTime.Parse(txtHanThanhToan.Text);
                if (txtMaDonDatHang.Text == "<F4 - Tra cứu>")
                {
                    txtMaDonDatHang.Text = "";
                }
                giatri.MaDonDatHang = txtMaDonDatHang.Text;
                string ngaydat = txtNgayDatHang.Text;
                if (ngaydat == string.Empty)
                {
                    ngaydat = date.ToString("MM/dd/yyyy");
                }
                giatri.NgayDat = DateTime.Parse(ngaydat);
                giatri.MaTienTe = "";// cbxTienTe.SelectedValue.ToString();
                giatri.TyGiaTienTe = "0";//txtTyGia.Text;
                giatri.ChietKhau = txtChietKhau.Text;
                if (txtThanhToanNgay.Text == string.Empty)
                {
                    txtThanhToanNgay.Text = "0";
                }
                giatri.ThanhToanNgay = txtThanhToanNgay.Text;
                giatri.ThueGTGT = txtThue.Text;
                giatri.TongTien = txtTongTien.Text;
                giatri.MaNhanVien = this.ManhanVien;
                giatri.TenNhanVien = this.TenNhanVien;
                giatri.GhiChu = txtGhichu.Text;
                giatri.ThanhToanSauKhiLapPhieu = "0";
                giatri.Deleted = false;
                giatri.DuNo = txtNoLai.Text;

                Entities.ChiTietHoaDonNhap[] cthdn = new Entities.ChiTietHoaDonNhap[DanhSach.Length];
                Entities.ChiTietKhoHang[] ctkh = new Entities.ChiTietKhoHang[DanhSach.Length];
                for (int i = 0; i < DanhSach.Length; i++)
                {
                    Entities.ChiTietKhoHang kho = new Entities.ChiTietKhoHang();
                    kho.HanhDong = DanhSach[i].HanhDong;
                    kho.MaKho = this.MaKho;
                    kho.TenKho = this.TenKho;
                    kho.MaHangHoa = DanhSach[i].MaHangHoa;
                    kho.TenHangHoa = DanhSach[i].TenHangHoa;
                    kho.SoLuong = DanhSach[i].SoLuong;
                    kho.NgayNhap = this.date.Date;
                    kho.HanSuDung = DateTime.Parse(DanhSach[i].HanSuDung);
                    kho.MaDonViTinh = DanhSach[i].MaDonViTinh;
                    kho.TenDonViTinh = DanhSach[i].TenDonViTinh;
                    kho.GhiChu = txtGhichu.Text;
                    kho.Deleted = false;
                    ctkh[i] = kho;

                    Entities.ChiTietHoaDonNhap row = new Entities.ChiTietHoaDonNhap();
                    row.HanhDong = DanhSach[i].HanhDong;
                    row.MaHoaDonNhap = this.MaHoaDonNhap;
                    row.MaHangHoa = DanhSach[i].MaHangHoa;
                    row.TenHangHoa = DanhSach[i].TenHangHoa;
                    row.SoLuong = DanhSach[i].SoLuong;
                    row.PhanTramChietKhau = DanhSach[i].ChietKhau;
                    row.DonGia = DanhSach[i].GiaNhap;
                    row.ThanhTien = DanhSach[i].TongDaChietKhau;
                    row.Thue = DanhSach[i].Thue;
                    row.SoLuongConLai = int.Parse(DanhSach[i].SoLuong);
                    row.HanSuDung = DateTime.Parse(DanhSach[i].HanSuDung);
                    row.MaDonViTinh = DanhSach[i].MaDonViTinh;
                    row.TenDonViTinh  = DanhSach[i].TenDonViTinh;
                    row.GhiChu = txtGhichu.Text;
                    row.Deleted = false;
                    cthdn[i] = row;
                }
                giatri.ChiTietHoaDon = cthdn;
                giatri.ChiTietKho = ctkh;
            }
            catch (Exception)
            {
                return null;
            }
            return giatri;
        }
        #endregion

        #region Tim Kiem
        private void ChiTietDonDatHang(string madondathang)
        {
            try
            {
                Entities.HienThiHangHoaListview ncc = new Entities.HienThiHangHoaListview();
                ncc.HanhDong = "5";
                ncc.MaKho = "";
                ncc.MaNhaCungCap = "";
                ncc.MaHoaDon = madondathang;
                DanhSach = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaCacBang(ncc);
                if (DanhSach != null)
                {
                    this.dgvHienThi.DataSource = this.DanhSach;
                }
                this.GridView();
            }
            catch (Exception)
            {
            }
        }

        private void HienThiDanhMuc(string mahanghoa)
        {
            try
            {
                Entities.HienThiHangHoaListview ncc = new Entities.HienThiHangHoaListview();
                ncc.HanhDong = "2";
                ncc.MaKho = "";
                ncc.MaNhaCungCap = "";
                ncc.MaHoaDon = "";
                Entities.HienThiHangHoaListview[] TimKiem = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaCacBang(ncc);
                if (TimKiem != null)
                {
                    for (int i = 0; i < TimKiem.Length; i++)
                    {
                        if (TimKiem[i].MaHangHoa == mahanghoa)
                        {
                            timhanghoa = TimKiem[i];
                            break;
                        }
                    }
                }
                TimKiem = null;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        private void HienThiTheoNhaCungCap(string mahanghoa)
        {
            try
            {
                Entities.HienThiHangHoaListview ncc = new Entities.HienThiHangHoaListview();
                ncc.HanhDong = "3";
                ncc.MaKho = "";
                ncc.MaNhaCungCap = txtMaNhaCungCap.Text;
                ncc.MaHoaDon = "";
                Entities.HienThiHangHoaListview[] TimKiem = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaCacBang(ncc);
                if (TimKiem != null)
                {
                    for (int i = 0; i < TimKiem.Length; i++)
                    {
                        if (TimKiem[i].MaHangHoa.ToUpper() == mahanghoa.ToUpper())
                        {
                            timhanghoa = TimKiem[i];
                            txtMaHangHoa.Text = this.timhanghoa.MaHangHoa;
                            txtGia.Text = this.timhanghoa.GiaNhap;;
                            txtThueVAT.Text = timhanghoa.Thue;
                            txtSoLuong.Focus();
                            Common.StaticVariable.ChiTietHangHoaCacBang = null;
                            break;
                        }
                    }
                    if (timhanghoa == null)
                    { this.ResetTool(); }
                }
                TimKiem = null;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        #endregion

        #region Tinh Toan
        //tinh thue gia tri gia tang
        private string TinhThue(string soluong, string gia, string thue)
        {
            string tralai = "0";
            try
            {
                if (float.Parse(thue) > 0)
                {
                    string tong = this.TinhTong(soluong, gia);
                    string th = ((double.Parse(thue) / 100) * double.Parse(tong)).ToString();
                    tralai = th;
                }
                else
                {
                    tralai = "0";
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                tralai = "0";
            }
            return tralai;
        }
        //tinh tong tien
        private string TinhTong(string soluong, string gia)
        {
            string tralai = "0";
            try
            {
                tralai = (Double.Parse(soluong) * Double.Parse(gia)).ToString();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                tralai = "0";
            }
            return tralai;
        }
        //tinh chiet khau
        private string TinhChietKhau(string tong, string phantramchietkhau)
        {
            string tralai = "0";
            try
            {
                if (float.Parse(phantramchietkhau) <= 0)
                {
                    tralai = "0";
                }
                else
                {
                    string ck = ((double.Parse(phantramchietkhau) / 100) * double.Parse(tong)).ToString();
                    tralai = ck;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                tralai = "0";
            }
            return tralai;
        }
        //tong tien trong danh sach hang hoa
        private string TongTien()
        {
            string tong = "0";
            try
            {
                Double giatri = 0;
                for (int i = 0; i < DanhSach.Length; i++)
                {
                    giatri += Double.Parse(DanhSach[i].TongDaChietKhau);
                }
                tong = giatri.ToString();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return tong;
        }
        private void HienThiTinhToan()
        {
            try
            {
                double tong = double.Parse(TongTien());
                double chietkhau = 0;
                double thue = 0;
                txtThanhTien.Text = tong.ToString() ;
                for (int i = 0; i < DanhSach.Length; i++)
                {
                    string thanhtien = TinhTong(DanhSach[i].SoLuong, DanhSach[i].GiaNhap);
                    chietkhau += double.Parse(TinhChietKhau(thanhtien, DanhSach[i].ChietKhau));
                    thue += double.Parse(TinhThue(DanhSach[i].SoLuong, DanhSach[i].GiaNhap, DanhSach[i].Thue));
                }
                txtChietKhau.Text = chietkhau.ToString();
                txtThue.Text = thue.ToString();
                string tinhno = TinhDuNo(txtNoHienThoi.Text,tong.ToString());
                txtTongTien.Text = tinhno;
                txtNoLai.Text = tinhno;
            }
            catch (Exception)
            {

                throw;
            }
        }
        private string TinhDuNo(string duno, string tongtien)
        {
            string tong = "0";
            try
            {
                tong = (Double.Parse(duno) + Double.Parse(tongtien)).ToString();
            }
            catch (Exception ex)
            { string s = ex.Message; tong = "0"; }
            return tong;
        }

        //tinh tong da chiet khau cua toan hoa don
        private string TongDaChietKhauHoaDon()
        {
            string tong = "0";
            try
            {
                double t = 0;
                for (int k = 0; k < this.DanhSach.Length; k++)
                {
                    t += double.Parse(this.DanhSach[k].TongDaChietKhau);
                }
                tong = t.ToString();
            }
            catch (Exception ex)
            { string s = ex.Message; tong = "0"; }
            return tong;
        }
        //tinh tong chua chiet khau cua toan hoa don
        private string TongChuaChietKhauHoaDon()
        {
            string tong = "0";
            try
            {
                double t = 0;
                for (int k = 0; k < this.DanhSach.Length; k++)
                {
                    t += double.Parse(this.DanhSach[k].TongChuaChietKhau);
                }
                tong = t.ToString();
            }
            catch (Exception ex)
            { string s = ex.Message; tong = "0"; }
            return tong;
        }
        //========================================================================================================
        #endregion

        #region Add
        private Boolean CapNhat(string soluong, string chietkhau)
        {
            try
            {
                rowlistview = new Entities.HienThiHangHoaListview();
                rowlistview.HanhDong = "Insert";
                rowlistview.MaKho = this.MaKho;
                rowlistview.MaHoaDon = this.MaHoaDonNhap;
                rowlistview.MaHangHoa = timhanghoa.MaHangHoa;
                rowlistview.TenHangHoa = timhanghoa.TenHangHoa;
                rowlistview.MaDonViTinh = timhanghoa.MaDonViTinh;
                rowlistview.TenDonViTinh = timhanghoa.TenDonViTinh;
                if (float.Parse(soluong) <= 0)
                {
                    txtSoLuong.Focus();
                    MessageBox.Show("Số lượng phải lớn hơn 0", "Thông báo");
                    return false;
                }
                rowlistview.SoLuong = soluong;
                rowlistview.GiaNhap = timhanghoa.GiaNhap;
                rowlistview.GiaBanBuon = timhanghoa.GiaBanBuon;
                rowlistview.GiaBanLe = timhanghoa.GiaBanLe;
                if (float.Parse(chietkhau) > 100)
                {
                    MessageBox.Show("Chiết khấu không được lớn hơn 100%", "Thông báo");
                    txtChietKhau.Text = "0";
                    txtChietKhau.Focus();
                    return false;
                }
                rowlistview.ChietKhau = chietkhau;
                rowlistview.Thue = timhanghoa.Thue;
                string tong = this.TinhTong(soluong, timhanghoa.GiaNhap);
                string tongdacothue = (double.Parse(tong) + double.Parse(this.TinhThue(soluong, timhanghoa.GiaNhap, timhanghoa.Thue))).ToString();
                rowlistview.TongDaChietKhau = (double.Parse(tongdacothue) - double.Parse(this.TinhChietKhau(tongdacothue, chietkhau))).ToString();
                rowlistview.TongChuaChietKhau = tong;
                DateTime dtHanSuDung = DateTime.Parse(txtHanSuDung.Text);
                if (DateTime.Compare(dtHanSuDung.Date, date.Date) < 0)
                {
                    txtHanSuDung.Text = date.ToString("MM/dd/yyyy");
                    txtHanSuDung.Focus();
                    MessageBox.Show("Hạn sử dụng phải lớn hơn hoặc bằng ngày hiện tại", "Thông báo");
                    return false;
                }
                rowlistview.HanSuDung = dtHanSuDung.ToString("MM/dd/yyyy");
                return true;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return false;
            }
        }

        private void Add()
        {
            try
            {
                if (CapNhat(txtSoLuong.Text, txtChietKhauNhaCungCap.Text) == false)
                {
                    return;
                }
                ArrayList banghi = new ArrayList();
                Boolean check = false;
                if (DanhSach != null)
                {
                    for (int i = 0; i < DanhSach.Length; i++)
                    {
                        Entities.HienThiHangHoaListview row = new Entities.HienThiHangHoaListview();
                        row.HanhDong = DanhSach[i].HanhDong;
                        row.MaHangHoa = DanhSach[i].MaHangHoa;
                        row.TenHangHoa = DanhSach[i].TenHangHoa;
                        string soluong = "0";
                        string tongdack = "0";
                        string tongchuack = "0";
                        if (DanhSach[i].MaHangHoa == rowlistview.MaHangHoa)
                        {
                            check = true;
                            soluong = (float.Parse(DanhSach[i].SoLuong) + float.Parse(rowlistview.SoLuong)).ToString();
                            tongdack = (double.Parse(DanhSach[i].TongDaChietKhau) + double.Parse(rowlistview.TongDaChietKhau)).ToString();
                            tongchuack = (double.Parse(DanhSach[i].TongChuaChietKhau) + double.Parse(rowlistview.TongChuaChietKhau)).ToString();
                        }
                        else
                        {
                            soluong = DanhSach[i].SoLuong;
                            tongdack = DanhSach[i].TongDaChietKhau;
                            tongchuack = DanhSach[i].TongChuaChietKhau;
                        }
                        row.SoLuong = soluong;
                        row.GiaNhap = DanhSach[i].GiaNhap;
                        row.GiaBanBuon = DanhSach[i].GiaBanBuon;
                        row.GiaBanLe = DanhSach[i].GiaBanLe;
                        row.ChietKhau = DanhSach[i].ChietKhau;
                        row.Thue = DanhSach[i].Thue;
                        row.TongDaChietKhau = tongdack;
                        row.TongChuaChietKhau = tongchuack;
                        row.HanSuDung = DanhSach[i].HanSuDung;
                        row.MaDonViTinh = DanhSach[i].MaDonViTinh;
                        row.TenDonViTinh = DanhSach[i].TenDonViTinh;
                        banghi.Add(row);
                    } 
                }
                if (check == false)
                {
                    banghi.Add(rowlistview);
                }
                int n = banghi.Count;
                if (n == 0) { DanhSach = null; }
                DanhSach = null;
                DanhSach = new Entities.HienThiHangHoaListview[n];
                for (int i = 0; i < n; i++)
                {
                    DanhSach[i] = (Entities.HienThiHangHoaListview)banghi[i];
                }
                HienThiTinhToan();
            }
            catch (Exception)
            {
            }
        }

        #endregion

        #region Quyền
        private Boolean Quyen()
        {
            try
            {
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        #endregion

        #region Thêm
        private void Them()
        {
            try
            {
                if (this.Quyen() == false)
                {
                    MessageBox.Show("Bạn không có quyền thêm","Thông báo");
                    return;
                }
                if (dgvHienThi.Rows.Count <= 0)
                { 
                    MessageBox.Show("Bạn phải thêm hàng hóa cho hóa đơn","Thông báo"); 
                    return; 
                }
                Entities.HoaDonNhap nk = this.GetValue("Insert");
                if (this.Validate(nk) == false)
                {
                    return;
                }
                this.MaHoaDonNhap = this.GetID();
                if (this.MaHoaDonNhap != nk.MaHoaDonNhap)
                {
                    nk.MaHoaDonNhap = this.MaHoaDonNhap;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateHoaDonNhap(nk);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = true;
                GetData(true);
                this.Close();

            }
            catch (Exception)
            {
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion
    }
}
#endregion
